/**
 * @param {string} input
 * @return {number}
 */
 var lengthLongestPath = function(input) {
  const tokens = input.split('\n');
  let stack = [];
  let ret = 0;
  let i = 0;
  while(i < tokens.length) {
      const c = tokens[i];
      if(c.startsWith('\t')) {
          const d = [...c.matchAll(/(\t)/g)].length;
          if (c.includes('.')) {
              stack.splice(d, stack.length - d);
              ret = Math.max(stack.join('/').length + c.length + 1 - d, ret);
          } else {
              stack.splice(d, stack.length - d, c.slice(d, c.length));
          }
      } else {
          if(c.includes('.')) {
              ret = Math.max(c.length, ret);
          } else {
            stack = [];
            stack.push(c);
          }
      }
      i++;
  } 
  return ret;
};

// console.log(lengthLongestPath("file1.txt\nfile2.txt\nlongfile.txt"))